package czh.cocoa.foundation;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NSArray extends NSObject implements Iterable<NSObject> {  

	public static NSArray array() {
		return new NSArray();
	}
	
	public static NSArray arrayWithArray(NSArray array) {
		return new NSArray(array);
	}
	
	public static NSArray arrayWithObjects(NSObject... objects) {
		NSArray array = new NSArray();
		for (NSObject o : objects) {
			array.mArray.add(o);
		}
		return array;
	}
	
	public static NSArray arrayWithObject(NSObject object) {
		NSArray array = new NSArray();
		array.mArray.add(object);
		return array;
	}
	
	List<NSObject> mArray;
	
	public NSArray() {  
		this(3);
	}
	
	public NSArray(NSArray array) {
		mArray = new ArrayList<NSObject>(array.mArray); 
	}
	
	public NSArray(int capacity) {
		mArray = new ArrayList<NSObject>(capacity);
	}

	public int indexOfObject(NSObject obj) {
		int index = mArray.indexOf(obj);
		return index != -1 ? index : NSNotFound;
	}
	
	public int count() {
		return mArray.size();
	}
	
	public boolean containsObject(NSObject obj) {
		return mArray.contains(obj);
	}
	
	public NSObject objectAtIndex(int index) {
		return mArray.get(index); 
	}
	
	public NSObject lastObject() {
		int count = mArray.size();
		return count > 0 ? mArray.get(count - 1) : null;
	}
	
	public NSObject firstObject() {
		int count = mArray.size();
		return count > 0 ? mArray.get(0) : null;
	}

	@Override
	public Iterator<NSObject> iterator() {
		return mArray.iterator();
//		return new Iterator<NSObject>() {
//			
//			int mIndex;
//
//			@Override
//			public boolean hasNext() {
//				return mIndex < mArray.size();
//			}
//
//			@Override
//			public NSObject next() {
//				return mArray.get(mIndex++);
//			}
//
//			@Override
//			public void remove() {
//				
//			}
//		};
	}
	
}
